const Router = require('koa-router')
const swiperRouter = new Router() 
const query = require('../utils/query')
var express = require('express')
var router = express.Router()
const send = require('koa-send')
//文件下载
var fs = require("fs")
var path = require("path")
var request = require("request")

swiperRouter.get('/api/download',async ctx => {
   // 设置头类型, 如果不设置，会直接下载该页面
   ctx.type = 'html';
   // 读取文件
   const pathUrl = path.join(__dirname, '/file/epet_back.rar');
   ctx.body = fs.createReadStream(pathUrl);
 });
  
 router.get('/fileload/:name', async (ctx) => {
   const name = ctx.params.name;
   const path = `static/upload/${name}`;
   ctx.attachment(path);
   await send(ctx, path);
});
swiperRouter.get('/get',async ctx => {
    const sql = `select * from swiper`
    let result = await query(sql)
    if(result.length > 0){
        ctx.body = {
            code: 200,
            msg: '获取轮播图成功',
            data: result
        }
    }else{
        ctx.body = {
            code: 201,
            msg: '数据库中无轮播图',
            data: []
        }
    }
})

module.exports = swiperRouter
